import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.bson.Document;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class 非经常性损益 {
    public static void main(String[] args) throws IOException {
        MongoCollection<Document> target = new MongoClient("192.168.1.175", 27017)
                .getDatabase("NFSD").getCollection("非经常性损益");
        InputStream input = new FileInputStream( "C:\\Users\\Administrator\\Desktop\\转换任务\\NFSD待转换(1)\\非经常性损益\\非经常性损益THS2008中报+2018-2019中、年报.xlsx" );
        XSSFWorkbook workbook = new XSSFWorkbook(input);
        XSSFSheet sheet = workbook.getSheetAt(0);
//        XSSFWorkbook workbook1 = new XSSFWorkbook();
//        XSSFSheet sheet1 = workbook1.createSheet();
        int rowNum = sheet.getLastRowNum();

        for (int i = 1; i <=rowNum; i++) {
            XSSFRow row = sheet.getRow(i);
//            String Code = row.getCell(0).getStringCellValue();

            if ( row != null){
                int coloumNum=row.getPhysicalNumberOfCells();
                if(coloumNum >2){
//                    System.out.println(coloumNum);
                    String code = row.getCell(0).getStringCellValue();
//                System.out.println(row.getCell(0).getStringCellValue());
                    String name = row.getCell(1).getStringCellValue();
//                    System.out.println(code);
//                    System.out.println(name);

                    for (int j = 2; j <=coloumNum; j++) {

                        if(row.getCell(j) != null){
                            String title = sheet.getRow(0).getCell(j).getStringCellValue();
//                            System.out.println(title);
                            String[] s_all = title.split("\n");
                            if(s_all.length == 4){
                                String timeR = s_all[1].replace("[报告期]","").trim();
                                String Id = code+"_"+ name+"_"+timeR;
//                                System.out.println(s_all.length);
//                                System.out.println(title);
                                String key =  GetKey(title);
                                if(key == null){
                                    key = s_all[0].trim();
                                }
                                Document document = new Document("_id",Id+"_"+key);
//                                System.out.println(Id);
//                                System.out.println(title);
                                document.append("code",code);
                                document.append("name",name);
                                document.append("报告期",timeR);
//                                String xiangmu = s_all[0].replace("存货明细－","").trim();
//                                document.append("存货项目",xiangmu);
//                                System.out.println(xiangmu);

                                Object value = row.getCell(j);
//                                System.out.println(title);
//                                System.out.println(value.toString());
                                document.append("指标",key);
                                document.append("value",value.toString());
                                target.updateOne(new BasicDBObject("_id", document.getString("_id")), new BasicDBObject("$set", document), (new UpdateOptions()).upsert(true));
                                System.out.println(document);
                            }

                        }

                    }
                }

            }

        }

    }
    public static String GetKey(String title){
        if(title.contains("归属于少数股东的非经常性损益净额")){
            return "归属于少数股东的非经常性损益净额";
        }else if(title.contains("归属于母公司所有者的非经常性损益净额")){
            return "归属于母公司所有者的非经常性损益净额";
        }else if(title.contains("非流动性资产处置损益")){
            return "非流动性资产处置损益";
        }else if(title.contains("偶发性")){
            return "偶发性损益";
        }else if(title.contains("政府补助")){
            return "政府补助";
        }else if(title.contains("资金占用费")){
            return "资金占用费";
        }else if(title.contains("享有被投资单位可辨认净资产公允价值产生的收益")){
            return "享有被投资单位可辨认净资产公允价值产生的收益";
        }else if(title.contains("非货币性资产交换损益")){
            return "非货币性资产交换损益";
        }else if(title.contains("委托他人投资或管理资产的损益")){
            return "委托他人投资或管理资产的损益";
        }else if(title.contains("资产减值准备")){
            return "偶发性资产减值准备";
        }else if(title.contains("债务重组损益")){
            return "债务重组损益";
        }else if(title.contains("企业重组费用")){
            return "企业重组费用";
        }else if(title.contains("对所得税影响额")){
            return "对所得税影响额";
        }else if(title.contains("公允价值变动损益")){
            return "公允价值变动损益";
        }else if(title.contains("应收款项减值准备转回")){
            return "应收款项减值准备转回";
        }else if(title.contains("对外委托贷款取得的损益")){
            return "对外委托贷款取得的损益";
        }else if(title.contains("托管费收入")){
            return "托管费收入";
        }else if(title.contains("非经常性损益")){
            return "非经常性损益";
        }else if(title.contains("企业合并")){
            return "企业合并净损益";
        }else if(title.contains("或有事项")){
            return "或有事项损益";
        }else if(title.contains("营业外")){
            return "其他营业外收支";
        }
//        else if(title.contains("")){
//            return "";
//        }
        return null;
    }
}
